using System;
using System.Collections;
using System.Data;
using System.Xml;

namespace LocalBoard.Persistence.MockAccessor
{
	/// <summary>
	/// Summary description for MockClubProfileAccessor.
	/// </summary>
	public class MockClubProfileAccessor : IClubProfileAccessor, IMockAccessor
	{
		private DataTable _clubXprofileTable = new DataTable("Club_x_Profile");
		public static readonly MockClubProfileAccessor Instance = 
			new MockClubProfileAccessor();

		private MockClubProfileAccessor()
		{
			_clubXprofileTable.Columns.Add("Club_Id", typeof(int));
			_clubXprofileTable.Columns.Add("Profile_Id", typeof(int));
			_clubXprofileTable.Columns.Add("ClubProfile", typeof(string));
		}

		public void ResetDataTable()
		{
			_clubXprofileTable.Clear();
		}

		public void RemoveRows(int tableIndex, DataRow[] rows)
		{
			foreach(DataRow row in rows)
				_clubXprofileTable.Rows.Remove(row);
		}

		public DataRow[] ExecuteFilter(string filterExpression)
		{
			return _clubXprofileTable.Select(filterExpression);
		}

		#region IClubProfileAccessor Members

		/// <summary>
		/// Check if the user is a member of the club or not
		/// </summary>
		/// <param name="id">User's profile id</param>
		/// <param name="clubId">Club id</param>
		/// <returns>True if user is a memeber of the club, False otherwise</returns>
		public bool IsUserInTheClub(int id, int clubId)
		{
			if(FindRow(id, clubId) != null)
				return true;
			else
				return false;
		}

		public Hashtable GetAllUserClubProfiles(int clubId)
		{
			Hashtable clubProfiles = new Hashtable();
			foreach(DataRow row in _clubXprofileTable.Select(string.Format("Club_Id={0}", clubId)))
			{
				XmlDocument document = null;
				string clubProfile = DataConverter.GetString(row["ClubProfile"]);
				if(clubProfile != null && clubProfile.Length > 0)
				{
					document = new XmlDocument();
					document.LoadXml(clubProfile);
				}
				clubProfiles.Add(DataConverter.GetInteger(row["Profile_Id"]), document);
			}
			return clubProfiles;
		}

		/// <summary>
		/// Get all club list that user joined
		/// </summary>
		/// <param name="profileId">profile id</param>
		/// <returns>Club list</returns>
		public ArrayList GetAllClubsThatUserJoined(int profileId)
		{
			ArrayList ar = new ArrayList();
			foreach(DataRow row in FindRow(profileId))
			{
				ar.Add(row["Club_Id"]);
			}
			return ar;
		}

		public XmlNode Get(int id, int clubId)
		{
			XmlDocument doc = null;
			DataRow row = FindRow(id, clubId);
			if(row != null)
			{
				string clubProfile = DataConverter.GetString(row["ClubProfile"]);
				if(clubProfile != null && clubProfile.Length > 0)
				{
					doc = new XmlDocument();
					doc.LoadXml(clubProfile);
				}
			}
			return doc;
		}

		public void Insert(int id, System.Xml.XmlNode clubProfile, int clubId)
		{
			DataRow newRow = _clubXprofileTable.NewRow();
			newRow["Profile_Id"] = id;
			newRow["Club_Id"] = clubId;
			newRow["ClubProfile"] = clubProfile != null ? clubProfile.OuterXml : "";
			_clubXprofileTable.Rows.Add(newRow);
			_clubXprofileTable.AcceptChanges();
		}

		public void Update(int id, System.Xml.XmlNode clubProfile, int clubId)
		{
			DataRow row = FindRow(id, clubId);
			if(row != null)
			{
				row["ClubProfile"] = clubProfile.OuterXml;
				_clubXprofileTable.AcceptChanges();
			}
		}

		public void Delete(int id, int clubId)
		{
			DataRow row = FindRow(id, clubId);
			if(row != null)
			{
				_clubXprofileTable.Rows.Remove(row);
				_clubXprofileTable.AcceptChanges();
			}
		}

		#endregion

		private DataRow FindRow(int profileId, int clubId)
		{
			DataRow[] rows = _clubXprofileTable.Select(string.Format("Profile_Id={0} and Club_Id={1}", profileId, clubId));
			if(rows.Length > 0)
			{
				return rows[0];
			}
			return null;
		}
		private DataRow[] FindRow(int profileId)
		{
			return _clubXprofileTable.Select(string.Format("Profile_Id={0}", profileId));
		}
	}
}
